/*
 	Pulse.h

 	Copyright (c) 2008 Bojan Rajkovic

 	Permission is hereby granted, free of charge, to any person obtaining a copy
 	of this software and associated documentation files (the "Software"), to deal
		in the Software without restriction, including without limitation the rights
 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 	copies of the Software, and to permit persons to whom the Software is
 	furnished to do so, subject to the following conditions:
	
 	The above copyright notice and this permission notice shall be included in
 	all copies or substantial portions of the Software.

 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 	THE SOFTWARE.
*/

#ifndef _PULSE_H_
#define _PULSE_H_ 1

#ifdef _HAVE_SQLITE3_
	#include "SQLiteWrapper.h"
#endif
#include "Utility.h"
#include "SocketWrapper.h"
#include "AllPurpose.h"
#include "Keycounter.h"

// set up defines for the pulse request format and the HTTP status codes that we'll be catching
#define PULSE_REQUEST "GET /pulse.php?username=%s&password=%s&keys=%li HTTP/1.1\r\nHost: %s\r\nUser-Agent: keycounter %s\r\n\r\n"
#define HTTP_STATUS_CODE_OK 200
#define HTTP_STATUS_CODE_MOVED_PERMANENTLY 301
#define HTTP_STATUS_CODE_FOUND 302
#define HTTP_STATUS_CODE_SEE_OTHER 303
#define HTTP_STATUS_CODE_MOVED_TEMPORARILY 307
#define HTTP_STATUS_CODE_BAD_REQUEST 400
#define HTTP_STATUS_CODE_UNATHORIZED 401
#define HTTP_STATUS_CODE_FORBIDDEN 402
#define HTTP_STATUS_CODE_NOT_FOUND 404 /* in essence, this and 410 are the same */
#define HTTP_STATUS_CODE_GONE 410
#define HTTP_STATUS_INTERNAL_ERROR 500
#define HTTP_STATUS_BAD_GATEWAY 502
#define HTTP_STATUS_SERVICE_UNAVAILABLE 503
#define HTTP_STATUS_VERSION_UNSUPPORTED 505

// pull in our namespaces
using namespace std;
using namespace pcrecpp;

typedef RE RegularExpression;

// define the PulseInfo struct that holds all the information needed for a pulse
class PulseInfo {
	
		private:
			unsigned long keys; /* number of keys since last pulse */
			std::string userName; /* user name for the pulse web service */
			std::string password; /* password for the pulse web service */
	
		public:
	
			PulseInfo(unsigned long Keys, std::string UserName, std::string Password) {
				this->keys = Keys;
				this->userName = UserName;
				this->password = Password;
			}
			
			unsigned long Keys() { return this->keys; }
			std::string UserName() { return this->userName; }
			std::string Password() { return this->password; }		
			
};

// define the pulse helper class
class Pulse {
	
		public:
			// pulses to the web service defined in the configuration file
			static unsigned int PulseToWeb(PulseInfo, std::string);
			// pulses to the database defined in the configuration file
			static unsigned int PulseToDB(PulseInfo, std::string);
			// handles the http error (other than 30x)
			static void HandleHTTPError(int);
			// zero the pulse file
			inline static void ZeroPulseFile();
			// save the pulse file
			static void SavePulseFile(unsigned long);
			// do the actual pulsing
			static bool DoPulse(unsigned long, std::string);
};

#endif
